package threads.thread2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class Ticket {

    public static AtomicInteger atomicInteger = new AtomicInteger(1000);

    public static void main(String[] args) {
        ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue(10);

        Runnable producer = () ->{
            while (true){
                try {
                    Integer ticket = atomicInteger.addAndGet(1);
                    System.out.println(Thread.currentThread().getName() + "生产第"+ ticket+ "张票");
                    blockingQueue.put(ticket);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(producer).start();
        new Thread(producer).start();

        Runnable consumer = () ->{
            while (true){
                try {
                    Integer ticket = blockingQueue.take();
                    System.out.println(Thread.currentThread().getName() +"消费第"+ ticket+ "张票");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(producer).start();
        new Thread(producer).start();

        new Thread(consumer).start();
        new Thread(consumer).start();
    }
}
